//**************************************************************************
//
//         (C) Okulov Rostislav, 2011
//
/*
 ***************************************************************************
 *                                                                         *
 *   This source is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This code is distributed in the hope that it will be useful, but      *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   General Public License for more details.                              *
 *                                                                         *
 *   A copy of the GNU General Public License is available on the World    *
 *   Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also      *
 *   obtain it by writing to the Free Software Foundation,                 *
 *   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.        *
 *                                                                         *
 */


import java.util.Random;
import javax.media.opengl.*;
import javax.media.opengl.glu.*;

//import com.jogamp.opengl.util.gl2.*;

public class r_grass {

        static final int grasscount = 100;
        private static Random random = new Random();

        static class CVert_quad {
            CVert_quad (float x, float y, float z,
                        float x1, float y1, float z1,
                        float x2, float y2, float z2,
                        float x3, float y3, float z3) {
                X = x;
                Y = y;
                Z = z;
                X1 = x1;
                Y1 = y1;
                Z1 = z1;
                X2 = x2;
                Y2 = y2;
                Z2 = z2;
                X3 = x3;
                Y3 = y3;
                Z3 = z3;
            }
            float X, Y, Z;
            float X1, Y1, Z1;
            float X2, Y2, Z2;
            float X3, Y3, Z3;
        }
       
        int[] angle = new int[grasscount];
        int trava_tex;
        double vrash;
        //float ElapsedTime;
        static CVert_quad[] travadata = new CVert_quad[grasscount];
       
        public r_grass(float size) {
               
                float k, l, r;
               
                for (int i=0;i<grasscount;i++) {
                    r = random.nextFloat();
                    k = r-0.5f;
                    l = r-0.5f;

                    travadata[i] = new CVert_quad(k,               0.001f, l,
                                                  k,        size + 0.001f, l,
                                                  size + k, size + 0.001f, l,
                                                  size + k,        0.001f, l);

                    angle[i] = random.nextInt(360);
                       
                }
                
        }


        public void draw(TVector position, GLAutoDrawable drawable) {

                GL gl = drawable.getGL();
               
                gl.glEnable(GL.GL_ALPHA_TEST);
                gl.glAlphaFunc(GL.GL_GREATER, 0.05f);
                gl.glPushMatrix();
                gl.glTranslatef(position.x, position.y, position.z);

                for (int i=0;i<grasscount;i++) {
                       
                    gl.glRotatef(angle[i], 0, 1, 0);
                    gl.glBegin(GL.GL_QUADS);
                    gl.glTexCoord2f(0.0f, 0.0f); gl.glVertex3f(travadata[i].X,  travadata[i].Y,  travadata[i].Z);
                    gl.glTexCoord2f(0.0f, 1.0f); gl.glVertex3d(travadata[i].X1, travadata[i].Y1, travadata[i].Z1 + vrash);
                    gl.glTexCoord2f(1.0f, 1.0f); gl.glVertex3d(travadata[i].X2, travadata[i].Y2, travadata[i].Z2 + vrash);
                    gl.glTexCoord2f(1.0f, 0.0f); gl.glVertex3f(travadata[i].X3, travadata[i].Y3, travadata[i].Z3);
                    gl.glEnd();
                       
                }
               
                gl.glPopMatrix();
                gl.glDisable(GL.GL_ALPHA_TEST);
        }
        
        public void animated(float ElapsedTime) {             
                         
        	float angle1 = ElapsedTime/60;
            double sin_travy = Math.sin(angle1/1);
            vrash = 0.001 * sin_travy;

        }
       
       
}
